<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="教师id" prop="teacherId">
      <el-input v-model="dataForm.teacherId" readonly placeholder="教师id"></el-input>
    </el-form-item>
    <el-form-item label="教师id" prop="teacherName">
      <el-input v-model="dataForm.teacherName" readonly placeholder="教师id"></el-input>
    </el-form-item>
    <el-form-item label="课程id" prop="courseId">
      <el-input v-model="dataForm.courseId" readonly placeholder="教师id"></el-input>
    </el-form-item>
    <el-form-item label="课程名" prop="courseName">
      <el-input v-model="dataForm.courseName" readonly placeholder="教师id"></el-input>
    </el-form-item>
    <el-form-item label="备注" >
      <el-input v-model="dataForm.remark" :readonly="!!dataForm.level" placeholder="备注"></el-input>
    </el-form-item>
    
    <el-form-item label="等级" prop="level">
      <el-rate :disabled="!!level" style="line-height:50px"
        v-model="dataForm.level"
        :colors="colors">
      </el-rate>
     
    </el-form-item>
   
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="!!level" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { evaluationteachingApi } from '@/api/teacher'

  export default {
    data () {
      return {
        visible: false,
        level:null,
        dataForm: {
          id: 0,
          teacherId: '',
          teacherName:'',
          courseName:'',
          courseId: '',
          level: null,
          remark: ''
        },
        colors: ['#99A9BF', '#F7BA2A', '#FF9900'] , // 等同于 { 2: '#99A9BF', 4: { value: '#F7BA2A', excluded: true }, 5: '#FF9900' }
        dataRule: {
          teacherId: [
            { required: true, message: '教师id不能为空', trigger: 'blur' }
          ],
          semester: [
            { courseId: true, message: '课程id不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '等级不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (course) {
        //重置表单，有部分属性没有使用poro所有使用表达的重制没效果
        this.dataForm = this.$options.data().dataForm;
        this.level = null
        this.dataForm.courseId = course.courseId
        this.dataForm.courseName = course.courseName
        this.dataForm.teacherId = course.teacherId
        this.dataForm.teacherName = course.teacherName
        this.visible = true
        evaluationteachingApi.getByTidAndSid(this.dataForm)
        .then(({et})=>{
          if(et){
            this.dataForm = et
            this.level =et.level
            this.dataForm.courseId = course.courseId
            this.dataForm.courseName = course.courseName
            this.dataForm.teacherId = course.teacherId
            this.dataForm.teacherName = course.teacherName
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            evaluationteachingApi.save(this.dataForm)
            .then(data => {
              if (data && data.code ==20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
